package leetcode_2401_2500;

import java.util.Arrays;
import java.util.Comparator;
import java.util.PriorityQueue;

public class LeeCode_2406 {
    public static void main(String[] args) {
        System.out.println(minGroups(new int[][]{
                {441459, 446342}, {801308, 840640}, {871890, 963447}, {228525, 336985}, {807945, 946787},
                {479815, 507766}, {693292, 944029}, {751962, 821744}}));

    }
    private static int minGroups(int[][] intervals) {
        Arrays.sort(intervals, Comparator.comparingInt(o -> o[0]));
        PriorityQueue<Integer> queue = new PriorityQueue<>(Comparator.comparingInt(o -> o));
        for (int[] interval : intervals) {
            if (queue.isEmpty()) {
                queue.add(interval[1]);
            } else {
                Integer p = queue.peek();
                if (p < interval[0]) {
                    queue.poll();
                }
                queue.add(interval[1]);
            }
        }
        return queue.size();
    }
}